/**
 * 
 */
package dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import model.LoaiDonViTCM;
import util.DAL;
import util.MyConnectUnit;

public class LoaiDonViTCD {
	MyConnectUnit conn;

	/**
	 * @function constructor LoaiDonViTCD
	 * @throws IOException
	 */
	public LoaiDonViTCD() throws Exception {
		this.conn = DAL.getDAL();
	}

	/**
	 * @function get all LoaiDonViTC
	 * @param Condition
	 * @param Limit
	 * @param OrderBy
	 * @return
	 * @throws Exception
	 */
	public ArrayList<LoaiDonViTCM> getAll(String Condition, String Limit, String OrderBy) throws Exception {
		ArrayList<LoaiDonViTCM> ls = new ArrayList<LoaiDonViTCM>();
		ResultSet rs = this.conn.Select("LOAI_DON_VI_TC", Condition, Limit, OrderBy);
		LoaiDonViTCM l = null;
		while (rs.next()) {
			l = new LoaiDonViTCM();
			l.setId_loai_don_vi_tc(rs.getInt("id_loai_don_vi_tc"));
			l.setTen(rs.getString("ten"));
			ls.add(l);
		}
		return ls;
	}

	/**
	 * @function get all LoaiDonViTC with Condition + Limit
	 * @param Condition
	 * @param Limit
	 * @return
	 * @throws Exception
	 */
	public ArrayList<LoaiDonViTCM> getAll(String Condition, String Limit) throws Exception {
		return getAll(Condition, Limit, null);
	}
	
	/**
	 * @function get all LoaiDonViTC with Condition
	 * @param Condition
	 * @return
	 * @throws Exception
	 */
	public ArrayList<LoaiDonViTCM> getAll(String Condition) throws Exception {
		return getAll(Condition, null);
	}

	/**
	 * @function get all LoaiDonViTC no parameter
	 * @return
	 * @throws Exception
	 */
	public ArrayList<LoaiDonViTCM> getAll() throws Exception {
		return getAll(null);
	}

	/**
	 * @function get id LoaiDonViTCM
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public LoaiDonViTCM getId(int id) throws Exception {
		LoaiDonViTCM ldv = new LoaiDonViTCM();
		ArrayList<LoaiDonViTCM> list = getAll("id_loai_don_vi_tc = " + id);
		if (list.size() > 0) {
			ldv.setId_loai_don_vi_tc(list.get(0).getId_loai_don_vi_tc());
			ldv.setTen(list.get(0).getTen());
			return ldv;
		}
		return null;
	}

	/**
	 * @function search ten LoaiDonViTC 
	 * @param ten
	 * @return
	 * @throws Exception
	 */
	public ArrayList<LoaiDonViTCM> searchLDV(String ten) throws Exception {
		ArrayList<LoaiDonViTCM> list = getAll("ten LIKE '%" + ten + "%'");
		return list;
	}

	/**
	 * @function get all LoaiDonViTC with search + paging
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<LoaiDonViTCM> getLimit(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<LoaiDonViTCM> list = getAll("ten LIKE '%" + ten + "%'", limit);
		return list;
	}
	
	/**
	 * @function insert LoaiDonViTC
	 * @param ldv
	 * @return
	 * @throws Exception
	 */
	public boolean Insert(LoaiDonViTCM ldv) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ten", ldv.getTen());
		return this.conn.Insert("LOAI_DON_VI_TC", map);
	}

	/**
	 * @function update LoaiDonViTC
	 * @param ldv
	 * @return
	 * @throws Exception
	 */
	public boolean Update(LoaiDonViTCM ldv) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ten", ldv.getTen());
		return this.conn.Update("LOAI_DON_VI_TC", map, "id_loai_don_vi_tc = " + ldv.getId_loai_don_vi_tc());
	}

	/**
	 * @function delete LoaiDonViTC
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean Delete(int id) throws Exception {
		return this.conn.Delete("LOAI_DON_VI_TC", "id_loai_don_vi_tc = " + id);
	}
}